package com.nuit.cxl;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test02 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {

        Test02 test = new Test02();
        test.newObject();
    }

    public void newObject() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {

        Class c1 = Class.forName("com.nuit.cxl.Person");
        Constructor constructor =  c1.getConstructor(String.class, int.class);
        //下面这两个都能构建  一样的对象
        Object p1 = constructor.newInstance("张三", 19);
        Person p2 = new Person("lisi", 20);
        System.out.println(p1);
        System.out.println(p2);

        Method method3 = c1.getDeclaredMethod("getName", null);
        method3.setAccessible(true);
        //method3.invoke(p1, null) 等同于 p1.getName()
        System.out.println(method3.invoke(p1, null));
    }

}
